package com.weareint.common.notify;

import cn.hutool.extra.mail.MailAccount;
import com.google.common.collect.Lists;
import com.weareint.common.config.ApplicationProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;

/**
 * <pre>
 *      发送电子邮件
 * </pre>
 */
@Slf4j
@Component
public class MailUtil {

    @Autowired
    private ApplicationProperties applicationProperties;

    /**
     * <pre>
     *      发送电子邮件
     * </pre>
     * @param tos 收件人列表
     * @param subject 邮件主题
     * @param content 邮件内容
     * @param isHtml 邮件内容是否是HTML
     * @param files 邮件中的附件
     */
    public void sendMail(List<String> tos, String subject, String content, boolean isHtml, File...files){
        ApplicationProperties.MailConfig mail = applicationProperties.getMail();
        MailAccount account = new MailAccount();
        account.setAuth(mail.isAuth())
                .setFrom(mail.getFrom())
                .setHost(mail.getHost())
                .setPort(mail.getPort())
                .setPass(mail.getPass())
                .setSslEnable(mail.isSsl())
                .setUser(mail.getUser())
                .setTimeout(mail.getTimeoutMs());
        cn.hutool.extra.mail.MailUtil.send(account, tos, subject, content, isHtml, files);
    }

    /**
     * <pre>
     *      发送普通的电子邮件
     * </pre>
     * @param to 收件人
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public void sendMail(String to, String subject, String content){
        sendMail(Lists.newArrayList(to), subject, content, false);
    }

    /**
     * <pre>
     *      发送电子邮件
     * </pre>
     * @param to 收件人
     * @param subject 邮件主题
     * @param content 邮件内容
     * @param isHtml 邮件内容是否是HTML
     * @param files 邮件中的附件
     */
    public void sendMail(String to, String subject, String content, boolean isHtml, File...files){
        sendMail(Lists.newArrayList(to), subject, content, false);
    }

}
